<?php

namespace App\Http\Controllers\Front;

use Illuminate\Http\Request;
use Session,Validator,Input;
use App\Models\User;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use App\Http\Requests\My\FindPwdRequest;
use App\Interfaces\UserService;
use Illuminate\Support\Facades\Redirect;


class FindPwdController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(User $user)
    {
        $step = "step_1";

        return view('front.my.find_pwd',compact('user','step'));
    }

    public function store(UserService $userService,Request $request)
    {

        $newPwd = Input::get('newPwd');
        $password_confirmation = Input::get('password_confirmation');
        $phone = Session::get('phone');
        $request = new FindPwdRequest();
        $rules = $request->rules();
        $messages = $request->messages();
        //dd($rules);
        $validator = Validator::make(Input::all(),$rules,$messages);

        //dd($validator);
        if($validator->passes()){

            $is_exit = User::where('phone', $phone)->first();

            $oldPwd = User::where('phone', $phone)->pluck('password');
            if($newPwd == $password_confirmation){
                if($is_exit){
                    if($oldPwd!=md5($newPwd)){
                        $user = $userService->findPaw($phone,$newPwd);
                        if($user){
                            return redirect::back()->with('step','step_3');
                        }else{
                            return redirect::back()->with('message','设置密码失败，请重新设置');
                        }

                    }else{
                        return redirect::back()->with('step','step_2')->with('message','新密码和原密码重复');
                    }

                } else{
                    return redirect::back()->with('message','该用户不存在');
                }

            }else{
                return redirect()->back()->with('step','step_2')->with('message','确认密码不匹配');

            }

        }else{

            return redirect()->back()->with('step','step_2')->with('message','请按规则填写');

        }
    }




}
